package org.valire.db;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

import dnl.toolbox.lang.ShellEnvironment;

/**
 * 
 * @author Daniel Orr
 * 
 */
public class JdbcInfoConfig {

	private String jdbcDriver;
	private String jdbcUrl;
	private String jdbcUser;
	private String jdbcPassword;
	private String driverJarPath;

	public JdbcInfoConfig(File propertiesFile) throws IOException {
		Properties properties = new Properties();
		properties.load(new FileReader(propertiesFile));
		loadProperties(properties);
		
	}
	
	public JdbcInfoConfig(Properties properties) throws IOException {
		loadProperties(properties);
	}

	private void loadProperties(Properties properties) {
		this.jdbcDriver = (String) properties.get("jdbc.driver");
		this.jdbcUrl = (String) properties.get("jdbc.url");
		this.jdbcUser = (String) properties.get("jdbc.user");
		this.jdbcPassword = (String) properties.get("jdbc.password");
		this.driverJarPath = (String) properties.get("driver.jar.path");
		this.driverJarPath = ShellEnvironment.replaceShellVariables(driverJarPath);
	}

	public JdbcConnectionInfo getJdbcConnectionInfo() {
		JdbcConnectionInfo ci = new JdbcConnectionInfo();
		ci.setDriver(jdbcDriver);
		ci.setJdbcUrl(jdbcUrl);
		ci.setUser(jdbcUser);
		ci.setPassword(jdbcPassword);
		ci.setDriverPath(driverJarPath);
		return ci;
	}

	public String getPathToDriver() {
		return driverJarPath;
	}

}
